var newWin;
var newForm;
var mainPanel;
var conn;
var win;
var menus;
var dayCostDao;
var resultDate;
var chartViewUrl='chartView.html';
var dataManageUrl='dataManage.html';
var dayCost = [{
	name : 'dcID',
	type : 'int'
}, {
	name : 'dc_year',
	type : 'int'
}, {
	name : 'dc_month',
	type : 'int'
}, {
	name : 'dc_day',
	type : 'int'
}, {
	name : 'dc_cost',
	type : 'int'
}];

var weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
function displaydate() {
	var today = new Date();
	
	var m=today.getMinutes();
	m=(m<10)?"0"+m:""+m;
	s=today.getSeconds();
	s=s<10?"0"+s:""+s;
	var s=today.getFullYear()+"年"+(today.getMonth()+1)+"月"+today.getDate()+"日&nbsp;" +weekday[today.getDay()]+"&nbsp;&nbsp;"+today.getHours()+":"+m+":"+s;
	
//	var s = weekday[today.getDay()] + "&nbsp;" + today.toLocaleString();
	Ext.get('timeView').update(s);

	setTimeout(function() {
		displaydate();
	}, 1000);
}

function exit() {
	Ext.Msg.show({
		title : 'Confirm Exit',
		msg : 'Are you sure you wish to exit day Management?',
		buttons : Ext.Msg.YESNO,
		fn : function(btn, text) {
			if (btn == "yes") {
				air.NativeApplication.nativeApplication.exit();
			}
		},
		animEl : 'elId',
		icon : Ext.MessageBox.QUESTION
	});
}

var actions = {
	charView : new Ext.Action({
		text : '图表浏览',
		handler : function() {
			mainPanel.load(chartViewUrl);
		}
	}),
	dataManage : new Ext.Action({
		text : '数据管理',
		handler : function() {
			mainPanel.load(dataManageUrl);
		}
	}),
	quit : new Ext.Action({
				text : 'Exit',
				handler : function() {
					exit();
				}
			}),
	about : new Ext.Action({
				text : 'About',
				handler : function() {
					Ext.MessageBox.alert('About day Management',
							'day Management version 1.0');
				}
			})
};

mainPanel=new Ext.Panel({
//	width:692,
//	height:330,
	border:false,
	bodyBorder:false,
	autoLoad:{
		url : chartViewUrl,
		nocache : true,
		scripts : true
	}
});

Ext.onReady(function() {
	win = new Ext.air.NativeWindow({
		id : 'mainWindow',
		instance : window.nativeWindow,
		minimizeToTray : true,
		trayIcon : 'icons/AIRApp_16.png',
		trayTip : 'day Manager',
		trayMenu : [{
					text : 'Open',
					handler : function() {
						// win.activate();
					}
				}, '-', {
					text : 'Exit',
					handler : function() {
						// win.activate();
						exit();
					}
				}]
	});

	win.on('closing', function(e) {
		e.preventDefault();
		// win.activate();
		exit();
	});

	menus = Ext.air.SystemMenu;
	menus.add('File', [actions.charView,actions.dataManage, '-', actions.quit]);
	menus.add('Help', [actions.about]);

	Ext.state.Manager.setProvider(new Ext.state.CookieProvider());
	
	mainPanel.render('indexMain');
	
	displaydate();
	// 数据库操作
	conn = Ext.sql.Connection.getInstance();
	conn.open('day_cost.db');

	conn.createTable({
				name : 'day_cost',
				key : 'dcID',
				fields : dayCost
			});

	dayCostDao = conn.getTable('day_cost', 'dcID');
	
});



